package com.huarun.module.webapi;

import com.alibaba.fastjson.JSONObject;
import com.huarun.common.util.Constant;
import com.huarun.common.util.LoginWelinkSigUtil;
import com.huarun.common.util.http.HttpParamers;
import com.huarun.common.util.http.HttpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2020-12-02
 * @Author: Lingnan
 **/
@Service
@Slf4j
public class WelingService {

    public String getToken(){
        String result = loginForWeiling();
        JSONObject resultJson = JSONObject.parseObject(result);
        String token = "";
        if(result != null && resultJson.getString("code").equals("0")){
            JSONObject jsonData = resultJson.getJSONObject("data");
            if(jsonData == null){
                return null;
            }
            token = jsonData.getString("token");
        }else {
            log.info("使用后台方式登录微瓴失败" + result);
        }
        return token;
    }
    public String[] getTokenAndIotimTicket(){
        String result = loginForWeiling();
        String[] returnStr = new String[2];
        JSONObject resultJson = JSONObject.parseObject(result);
        if(result != null && resultJson.getString("code").equals("0")){
            JSONObject jsonData = resultJson.getJSONObject("data");
            if(jsonData == null){
                return null;
            }
            returnStr[0] = jsonData.getString("token");
            returnStr[1] = jsonData.getString("iotim_ticket");
        }else {
            log.info("使用后台方式登录微瓴失败" + result);
        }
        return returnStr;
    }
    private String loginForWeiling(){
        Long time = System.currentTimeMillis();
        String appid = Constant.WELINK_APP_ID;
        String appTicket = Constant.WELINK_APP_TICKET;
        String projetId = Constant.WELINK_APP_PROJECTID;
        Long num = new Double(Math.random() * 1000).longValue();
        // 生成sig
        String sig = LoginWelinkSigUtil.encrypt(Constant.WELINK_APP_KEY, time, num);
        /** 2. 调用微瓴接口登录 **/
        String uri = Constant.WELINK_LOGIN_URI;
        String host = Constant.WELINK_LOGIN_HOST;
        String url = uri + "?appid=" + appid + "&time=" + time + "&num=" + num + "&sig=" + sig + "&app_ticket="
                + appTicket + "&projetId=" + projetId;
        HttpParamers paramers = HttpParamers.httpGetParamers();
        paramers.addParam("appid",appid);
        paramers.addParam("time",time);
        paramers.addParam("num",num);
        paramers.addParam("sig",sig);
        paramers.addParam("app_ticket",appTicket);
        paramers.addParam("projetId",projetId);
        HttpService httpService = new HttpService(host);
        String result = null;
        try {
            result = httpService.service("/common/ticket/loginByApp",paramers);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("使用后台方式登录微瓴请求失败" + e.toString());
        }
        log.info("使用后台方式登录微瓴返回：" + result);
        return result;
    }
}
